package com.hdyg.sixg.activity.home;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.allen.library.SuperTextView;
import com.hdyg.sixg.R;
import com.hdyg.sixg.activity.common.BaseActivity;
import com.hdyg.sixg.adapter.HostTypeAdapter;
import com.hdyg.sixg.adapter.ManageBiAdapter;
import com.hdyg.sixg.entry.HostBean;
import com.hdyg.sixg.entry.ManageMoneyBean;
import com.hdyg.sixg.mvp.Contrant.CHost;
import com.hdyg.sixg.mvp.base.BaseUrl;
import com.hdyg.sixg.mvp.base.GetParamUtil;
import com.hdyg.sixg.mvp.base.SpMsg;
import com.hdyg.sixg.mvp.presenter.PHost;
import com.hdyg.sixg.util.PopWindowUtil;
import com.hdyg.sixg.util.StringUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.List;

import butterknife.BindView;


/**
 * @author CZB
 * @describe 投资理财
 * @time 2019/4/8 10:58
 */
public class HostActivity extends BaseActivity implements CHost.IVHost {


    private final static int REQUEST_CODE = 0x00001;
    @BindView(R.id.ll_top_left)
    LinearLayout llTopLeft;
    @BindView(R.id.tv_top_right)
    TextView tvTopRight;
    @BindView(R.id.ll_top_right)
    LinearLayout llTopRight;
    @BindView(R.id.tvTopTitle)
    TextView tvTopTitle;
    @BindView(R.id.tv_role)
    TextView tvRole;
    @BindView(R.id.sv_type)
    SuperTextView svType;
    @BindView(R.id.sv_balance)
    SuperTextView svBalance;
    @BindView(R.id.et_host_number)
    EditText etHostNumber;
    @BindView(R.id.et_pay_pwd)
    EditText etPayPwd;
    @BindView(R.id.bt_sure)
    Button btSure;
    @BindView(R.id.tv_record)
    TextView tvRecord;
    @BindView(R.id.rv_record)
    RecyclerView rvRecord;
    @BindView(R.id.srl_refresh)
    SmartRefreshLayout srlRefresh;

    private CHost.IPHost mPresenter;
    private HostTypeAdapter mAdapter;

    private List<ManageMoneyBean.CoinsBean> coinBeans;//币类型数据
    private List<HostBean.DataBean> dataBeans;//记录数据
    private ManageMoneyBean.RuleBean ruleBean;
    private String cointype, amount, trapass;
    private int page = 1, allDataSize;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_host;
    }

    @Override
    protected void initView() {
        onClickListener();
        tvTopTitle.setText(R.string.main_smart);
        tvTopRight.setText(R.string.manage_income_record);
        rvRecord.setLayoutManager(new LinearLayoutManager(mContext));
    }

    private void onClickListener() {
        srlRefresh.setOnRefreshListener(refreshLayout -> {
            srlRefresh.finishRefresh(2000);//传入false表示刷新失败
            page = 1;
            initData();
        });
        llTopLeft.setOnClickListener(v -> finish());
        //选择币种类型
        svType.setOnSuperTextViewClickListener(superTextView -> {
            showCoinTypePop(superTextView);
        });
        //规则
        tvRole.setOnClickListener(v -> {
            if (ruleBean == null) {
                return;
            }
            mBundle = new Bundle();
            mBundle.putSerializable(SpMsg.INTENT_STR, ruleBean);
            startActivity(ManageRuleActivity.class, mBundle);
        });
        //收益记录
        llTopRight.setOnClickListener(v -> {
            startActivity(IncomeRecordActivity.class);
        });
        //立即托管
        btSure.setOnClickListener(v -> {

            amount = etHostNumber.getText().toString().trim();
            trapass = etPayPwd.getText().toString().trim();
            if (TextUtils.isEmpty(cointype)) {
                toastNotifyShort(R.string.toast_bi_type);
                return;
            }
            if (TextUtils.isEmpty(amount)) {
                toastNotifyShort(R.string.host_number_hint);
                return;
            }
            if (TextUtils.isEmpty(trapass)) {
                toastNotifyShort(R.string.pay_pwd_hint);
                return;
            }
            if (StringUtil.isFastDoubleClick()) {
                return;
            }
            mPresenter.pGetStartHost(BaseUrl.START_DEPOSIT_URL, GetParamUtil.getStartDepositParam(cointype, amount, trapass));
        });
    }

    @Override
    protected void initData() {
        mPresenter.pGetInit(BaseUrl.DEPOSIT_URL, GetParamUtil.getDepositParam(page));
    }

    //显示币种悬浮窗
    private void showCoinTypePop(View v) {
        if (coinBeans == null) {
            toastNotifyShort(R.string.toast_coin);
            return;
        }
        View view = View.inflate(mContext, R.layout.pop_cash_bi_coin, null);
        RecyclerView rvArea = view.findViewById(R.id.rv_coin);
        rvArea.setLayoutManager(new LinearLayoutManager(mContext));
        ManageBiAdapter areaAdapter = new ManageBiAdapter(R.layout.item_cash_bi_coin, coinBeans);
        rvArea.setAdapter(areaAdapter);
        areaAdapter.setOnItemClickListener((adapter, view1, position) -> {
            cointype = coinBeans.get(position).getId();
            svType.setRightString(coinBeans.get(position).getEn_name()).setRightTextColor(getResources().getColor(R.color.white));
            svBalance.setRightString(coinBeans.get(position).getBalance());
            mPopupWindow.dismiss();
        });
        mPopupWindow = PopWindowUtil.getInstance().makePopupWindow(mContext, view, ViewGroup.LayoutParams.MATCH_PARENT)
                .showAsDropDown(mContext, v, 0);

    }

    @Override
    protected void createPresenter() {
        mPresenter = new PHost(this);
    }

    private void initAdapter() {
        mAdapter = new HostTypeAdapter(R.layout.item_host_record, dataBeans);
        rvRecord.setAdapter(mAdapter);
        mAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            int id = view.getId();
            if (id == R.id.tv_right_bottom) {
                mBundle = new Bundle();
                mBundle.putString(SpMsg.INTENT_STR, mAdapter.getData().get(position).getId());
                startActivityForResult(MnageTransActivity.class, mBundle, REQUEST_CODE);
            }
        });
        mAdapter.setOnLoadMoreListener(() -> {
            if (mAdapter.getData().size() < allDataSize) {
                page++;
                initData();
            } else {
                mAdapter.loadMoreEnd();
            }
        }, rvRecord);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }
        switch (requestCode) {
            case REQUEST_CODE:
                page = 1;
                initData();
                break;
        }
    }

    @Override
    public void vGetInitSuccess(HostBean dataBean) {
        dataBeans = dataBean.getList();
        ruleBean = dataBean.getRules();
        allDataSize = dataBean.getTotal();//总条数
        if (coinBeans == null) {
            coinBeans = dataBean.getCointype();
        }
        if (page == 1) {
            if (mAdapter != null) {
                mAdapter.setNewData(dataBeans);
            } else {
                if (dataBeans == null || dataBeans.size() == 0) {
                    tvRecord.setVisibility(View.GONE);
                } else {
                    tvRecord.setVisibility(View.VISIBLE);
                }
                initAdapter();
            }
        } else {
            if (dataBeans.size() != 0) {
                mAdapter.addData(dataBeans);
            }
            mAdapter.loadMoreComplete();
        }
    }

    @Override
    public void vGetStartHostSuccess(String dataBean) {
        toastNotifyShort(dataBean);
        setResult(RESULT_OK);
        finish();
    }

}
